#include "stm32f10x.h"                  // Device header

void Timer_Int2_Init(void)  //基准计数时钟(内部时钟)的输入频率为72000KHz
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2使能
	TIM_InternalClockConfig(TIM2); //选择内部时钟作为时钟源，可省略
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 10000-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	TIM_ClearFlag(TIM2,TIM_FLAG_Update); //避免初始值为1
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允许更新中断(是计时器中断的一种)
	//更新频率为1Hz
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM2,ENABLE); //启动计时器
}


void Timer_Int3_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM2使能
	TIM_InternalClockConfig(TIM3); //选择内部时钟作为时钟源，可省略
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 10000-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 14400-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	TIM_ClearFlag(TIM3,TIM_FLAG_Update); //避免初始值为1
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许更新中断(是计时器中断的一种)
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM3,ENABLE); //启动计时器
}

void Timer_Int4_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM4使能
	TIM_InternalClockConfig(TIM4); //选择内部时钟作为时钟源，可省略
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 10000-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
	
	TIM_ClearFlag(TIM4,TIM_FLAG_Update); //避免初始值为1
	TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); //允许更新中断(是计时器中断的一种)
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM4,ENABLE); //启动计时器
}

void Timer_Ext2_Init(void)  // PA0从硬件被定义为TIM2-EXT
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2使能
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//选择外部时钟作为时钟源
	//TIM_ExtTRGPSC_OFF外部触发预分频器不分频
	//TIM_ExtTRGPolarity_NonInverted外部触发极性为不反向(高电平或上升沿有效)
	//0x00不滤波
	TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x00);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 10-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 1-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	TIM_ClearFlag(TIM2,TIM_FLAG_Update); //避免初始值为1
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允许更新中断(是计时器中断的一种)
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM2,ENABLE); //启动计时器
}

/* 中断函数模版
void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update) != SET)
		return;
	TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
*/
